Skip to content

feat(core): add global context map to Agent interface#1207

Open
KFCFans wants to merge 3 commits intoagentscope-ai:mainfrom
KFCFans:feat/agent-context-map
Open

feat(core): add global context map to Agent interface#1207
KFCFans wants to merge 3 commits intoagentscope-ai:mainfrom
KFCFans:feat/agent-context-map

Conversation

@KFCFans
Copy link
Copy Markdown

@KFCFans KFCFans commented Apr 13, 2026

AgentScope-Java Version

1.0.11

Description

Background

When building complex agent applications with hooks, there is currently no built-in mechanism for hooks and other components to share custom data throughout the agent execution lifecycle. For example, a PreReasoningEvent hook might need to pass contextual information (e.g., request metadata, tracing context, feature flags) to a PostActingEvent hook, but there is no standard way to do so without resorting to external state management like ThreadLocal or static maps, which are fragile in reactive/async environments.

Changes

This PR adds a getContext() method to the Agent interface, providing a mutable Map<String, Object> that is scoped to each agent instance. This allows hooks and other components to share custom data across the entire agent execution lifecycle in a thread-safe manner.

Specific changes:

  • Agent.java: Added Map<String, Object> getContext() method to the interface with Javadoc.
  • AgentBase.java: Added a ConcurrentHashMap-backed context field, initialized in the constructor. The getContext() method is declared final to ensure consistent behavior across all agent implementations.

Usage Example

// In a PreReasoning hook — store data
agent.getContext().put("request_id", requestId);
agent.getContext().put("start_time", System.currentTimeMillis());

// In a PostActing hook — retrieve data
String requestId = (String) agent.getContext().get("request_id");
long elapsed = System.currentTimeMillis() - (long) agent.getContext().get("start_time");

Design Decisions

  • ConcurrentHashMap: Chosen for thread safety since hooks may execute on different reactor threads.
  • final method: Prevents subclasses from overriding the context behavior, ensuring a single consistent implementation.
  • Instance-scoped: Each agent has its own context map, avoiding cross-agent interference.

Checklist

  • Code has been formatted with mvn spotless:apply
  • All tests are passing (mvn test)
  • Javadoc comments are complete and follow project conventions
  • Related documentation has been updated (e.g. links, examples, etc.)
  • Code is ready for review

Provide a mutable Map<String, Object> context on the Agent interface,
allowing hooks and other components to share custom data throughout
the agent execution lifecycle.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@KFCFans KFCFans requested a review from a team April 13, 2026 04:13
@CLAassistant
Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


朱八 seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

Add the missing getContext() implementation to the TestAgent stub
in JsonlTraceExporterTest, required after the new method was added
to the Agent interface.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@codecov
Copy link
Copy Markdown

codecov bot commented Apr 13, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Verify that the context map is non-null, initially empty, supports
put/get, and is isolated per agent instance.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@LearningGp
Copy link
Copy Markdown
Collaborator

image

@KFCFans
Copy link
Copy Markdown
Author

KFCFans commented Apr 13, 2026

image

DONE,已经签了。
不过 commit 的账号是公司账号 @taobao.com,逻辑上好像天然也不存在问题?hhh

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants